﻿	package app.demo.MyTouchApp{
	import flash.events.TUIO; // allows to connect to touchlib/tbeta
 
    import flash.display.*;
    import flash.events.*;
	import flash.events.MouseEvent;
    import flash.geom.*; // needed for curPt;
	import flash.display.SimpleButton;
	import flash.net.URLRequest;
	import flash.display.FrameLabel;
	import fl.controls.Label;
	import fl.controls.*;
	import fl.core.*;
	//import mx.controls.Label;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.system.fscommand;
	import flash.system.*;
	import flash.events.IOErrorEvent;


	
	public class master extends MovieClip {
	//private static var instance:master;
	//var movie0:MovieClip = new MovieClip();
	//var movie1:MovieClip = new MovieClip();
	var pageKeyboard:keyBoard;
	var pageSelectTopic:selectTopic;
	var mainpage:MyTouchApp;
	var avatarpage:selectAvatar;
	var wellDonePage:wellDone;
	var topicPage:topicMatching;
	var gameT:topicGame;
	var instructionsPage:instructionsTopic;
	var theText:String;
	var theIndex:int;
	var shapesPage:shapesInAHole;
	var playersPage:numberOfPlayers;
	var avatarPage2:selectAvatar2;
	var keyboardTwo:keyboard2;
	var pageSelectBoard:selectBoard;
	var gameShapes:shapesGame;
	var avatarPlayer1:avatarPlayerOne;
	var avatarPlayer2:avatarPlayerTwo;
	var pageSelectBoard2:selectBoardTwoPlayers;
	var keyboardp1:keyBoard1stPlayer;
	var keyboardp2:keyBoard2ndPlayer;
	var gameTwoPlayers: shapesGameP2;
	var wellDoneOneP:wellDoneOnePlayer;
	var instructionsPageShapes:instructionsShapes;
	var gameLetters:lettersGame;
	var animalsT:topicAnimals;
	var natureT:topicNature;
	var fruitsT:topicFruits;
	var wellD:wellDoneTwoPlayers;
	//var randomIndex:Number= randomNumber(0,9);
	
	/////////////////////////////////////////
	//Knowing the avatars & names of the two players:
	var indexP1:int;
	var indexP2:int;
	var theText1:String;
	var modeP=1;
	var theText2:String;
	/////////////////////////////////////////
	
	var scoreIndex:int;
		
	
	public function master(){
			//instance = this;
			
			//--------connect to TUIO-----------------
			TUIO.init(this,'localhost',3000,'',true);
            trace("MyPage Initialized");
			
			mainpage= new MyTouchApp(this);
			addChild(mainpage);
			
			theText="Player 1";
			theText1="Player 1";
			theText2="Player 2";
			
			pageSelectTopic = new selectTopic(this);

			avatarpage= new selectAvatar(this);
			pageKeyboard= new keyBoard(this);

			topicPage= new topicMatching(this);
			shapesPage= new shapesInAHole(this);
			
			playersPage= new numberOfPlayers(this);
			avatarPage2= new selectAvatar2(this);
			keyboardTwo= new keyboard2(this);
			pageSelectBoard= new selectBoard(this);
			avatarPlayer1 = new avatarPlayerOne(this);
			avatarPlayer2 = new avatarPlayerTwo(this);
			pageSelectBoard2 = new selectBoardTwoPlayers(this);
			keyboardp1= new keyBoard1stPlayer(this);
			keyboardp2= new keyBoard2ndPlayer(this);
			
			
			
			//theIndex=randomIndex(1,9) as int;
			//theIndex= 4;
			theIndex=Math.round(Math.random() * (9 - 0)) + 0;
			
			/////////////////////////////////////////////////
			//Choosing the avatar randomly for both players:
			indexP1=Math.round(Math.random() * (9 - 0)) + 0;
			indexP2=Math.round(Math.random() * (9 - 0)) + 0;
			/////////////////////////////////////////////////
			
			fscommand("allowscale", "false");
			fscommand("fullscreen", "true");
			//Stage.showDefaultContextMenu = false;
			
			scoreIndex=-1;
			stage.scaleMode ="noScale";
			stage.displayState="fullScreen";
	}
	
	public function goToMain()
	{
		//removeChild(mainpage);
		removeChild(mainpage);
		addChild(topicPage);
	}

	
	public function goToFirstPage()
	{
		//removeChild(mainpage);
		removeChild(topicPage);
		addChild(mainpage);
	}
	
	
	public function goToAvatar()
	{
		//removeChild(mainpage);
		removeChild(topicPage);
		addChild(avatarpage);
	}
	
	
	public function goBackMain()
	{
		removeChild(avatarpage);
		//addChild(mainpage);
		addChild(topicPage);
	}
	
	
	public function goToChooseName()
	{
		removeChild(avatarpage);
		addChild(pageKeyboard);
		trace("Index in master is");
		trace(avatarpage.index2);
		theIndex=avatarpage.index2;
	}
	public function getTheIndex():int
	{
		trace("The index when called is");
		trace(theIndex);
		return theIndex;
	}
	
	public function nextTopic()
	{
		gameT= new topicGame(this);
		removeChild(pageSelectTopic);
		addChild(gameT);
	}
	
	public function backTopic()
	{
		
		removeChild(gameT);
		addChild(pageSelectTopic);
	}
	
	
	public function goBackAvatar()
	{
		removeChild(pageKeyboard);
		addChild(avatarpage);
	}
	
	
	public function goToSelectTopic()
	{
		removeChild(pageKeyboard);
		addChild(pageSelectTopic);
		trace("Text in master is");
		trace(pageKeyboard.Text);
		theText=pageKeyboard.Text;
	}
	public function getTheText():String
	{
		trace("The text when called is");
		trace(theText);
		return theText;
	}
	
	public function goBackChooseName()
	{
		removeChild(pageSelectTopic);
		addChild(pageKeyboard);
	}
	
	public function goToWellDone()
	{
		wellDonePage= new wellDone(this);
		removeChild(gameT);
		addChild(wellDonePage);
	}
	
	public function goBackToMain()
	{
		removeChild(wellDonePage);
		addChild(mainpage);
	}
	
	public function goToInstructions()
	{
		instructionsPage= new instructionsTopic(this);
		removeChild(topicPage);
		addChild(instructionsPage);
	}
	
	public function playAgain()
	{
		removeChild(wellDonePage);
		addChild(topicPage);
	}
	
	public function goToGame()
	{
		trace("theText is:::::");
		trace(theText);
		trace("theIndex is::::");
		trace(theIndex);
		//theIndex=avatarpage.index2;
		gameT= new topicGame(this);
		animalsT= new topicAnimals(this);
		removeChild(avatarpage);
		addChild(pageSelectTopic);
	}
	
	public function backInstructions()
	{
		//instructionsPage= new instructionsTopic(this);
		removeChild(instructionsPage);
		addChild(topicPage);
	}
	
	public function goToAnimalsGame()
	{
		animalsT= new topicAnimals(this);
		removeChild(pageSelectTopic);
		addChild(animalsT);
	}
	public function backTopicB1()
	{
		removeChild(animalsT);
		addChild(pageSelectTopic);
	}
	public function goToNatureGame()
	{
		natureT= new topicNature(this);
		removeChild(pageSelectTopic);
		addChild(natureT);
	}
	public function backTopicB2()
	{
		removeChild(natureT);
		addChild(pageSelectTopic);
	}
	public function goToFruitGame()
	{
		fruitsT= new topicFruits(this);
		removeChild(pageSelectTopic);
		addChild(fruitsT);
	}
	public function backTopicB3()
	{
		removeChild(fruitsT);
		addChild(pageSelectTopic);
	}
	public function goToWellDone1()
	{
		wellDonePage= new wellDone(this);
		removeChild(animalsT);
		addChild(wellDonePage);
	}
	
	public function goToWellDone4()
	{
		wellDonePage= new wellDone(this);
		removeChild(natureT);
		addChild(wellDonePage);
	}
	public function goToWellDone3()
	{
		wellDonePage= new wellDone(this);
		removeChild(fruitsT);
		addChild(wellDonePage);
	}
	
	////////////////////////////////////////////////
	//Shapes in a hole:
	
	public function goToShapesMain()
	{
		removeChild(mainpage);
		addChild(shapesPage);
	}
	
	public function goToFirstPage1()
	{
		removeChild(shapesPage);
		addChild(mainpage);
	}
	
	public function goBackShapesMain()
	{
		removeChild(playersPage);
		addChild(shapesPage);
	}
	
	public function goToAvatarTwo()
	{
		removeChild(playersPage);
		addChild(avatarPage2);
	}
	
	public function goToPlayers()
	{
		removeChild(shapesPage);
		addChild(playersPage);
	}
	
	public function backToPlayers()
	{
		removeChild(avatarPage2);
		addChild(playersPage);
	}
	
	public function goToKeyboard()
	{
		removeChild(avatarPage2);
		addChild(keyboardTwo);
		trace("Index of avatar is: ");
		theIndex=avatarPage2.index2;
		trace(theIndex);
	}
	
	public function goBackToAvatar()
	{
		removeChild(keyboardTwo);
		addChild(avatarPage2);
		}
	
	
	public function goToSelectBoard()
	{
		removeChild(keyboardTwo);
		addChild(pageSelectBoard);
		theText=keyboardTwo.Text;
	}
	public function goBackToKeyboard()
	{
		removeChild(pageSelectBoard);
		addChild(keyboardTwo);
	}
	public function goToBoard()
	{
		gameShapes= new shapesGame(this);
		removeChild(pageSelectBoard);
		addChild(gameShapes);
	}
	
	public function backBoard()
	{
		removeChild(gameShapes);
		addChild(pageSelectBoard);
	}
	
	public function goToGameBoard()
	{
		removeChild(avatarPage2);
		addChild(pageSelectBoard);
	}
	
	public function goToAvatarPlayer1()
	{
		removeChild(playersPage);
		addChild(avatarPlayer1);
	}
	
	public function backToPlayersPage()
	{
		removeChild(avatarPlayer1);
		addChild(playersPage);
	}
	
	public function skipPlayer1GoToPlayer2()
	{
		removeChild(avatarPlayer1);
		addChild(avatarPlayer2);
	}
	
	public function backToAvatarPlayers()
	{
		removeChild(avatarPlayer2);
		addChild(playersPage);
	}
	public function skipPlayer2GoToGameBoard()
	{
		
		removeChild(avatarPlayer2);
		gameTwoPlayers= new shapesGameP2(this);
		addChild(gameTwoPlayers);
		//addChild(pageSelectBoard2);
		//indexP1=avatarPlayer1.avatarP1;
		//indexP2=avatarPlayer2.avatarP2;
		//getIndexP1();
		//getIndexP2();
	}
	
	public function goToKeyboardP1()
	{
		removeChild(avatarPlayer1);
		addChild(keyboardp1);
		indexP1=avatarPlayer1.avatarP1;
	
	}
	
	public function goBackToAvatarP1()
	{
		removeChild(keyboardp1);
		addChild(avatarPlayer1);
	}
		
	public function goTo2ndPlayer()
	{
		modeP=2;
		removeChild(keyboardp1);
		//addChild(playersPage);
		addChild(avatarPlayer2);
		theText1=keyboardp1.Text;
	}
	
	public function goToKeyboardP2()
	{
		removeChild(avatarPlayer2);
		addChild(keyboardp2);
		indexP2=avatarPlayer2.avatarP2;
	}
		
	public function goBackToAvatarP2()
	{
		removeChild(keyboardp2);
		addChild(avatarPlayer2);
	}
	
	public function goToSelectBoard2()
	{
		theText2=keyboardp2.Text;
		//shapesPage= new shapesInAHole(this);
		removeChild(keyboardp2);
		//addChild(playersPage);
		//addChild(pageSelectBoard2);
		gameTwoPlayers= new shapesGameP2(this);
		addChild(gameTwoPlayers);
		
	}
	
	public function goBackToKeyboard2()
	{
		removeChild(pageSelectBoard2);
		addChild(playersPage);
	}
	
	public function goToBoardPlayers()
	{
		gameTwoPlayers= new shapesGameP2(this);
		removeChild(pageSelectBoard2);
		addChild(gameTwoPlayers);
	}
	
	public function goToWellDone2()
	{
		wellDoneOneP= new wellDoneOnePlayer(this);
		removeChild(gameShapes);
		addChild(wellDoneOneP);
	}
	
	public function playAgain1()
	{
		removeChild(wellDoneOneP);
		addChild(shapesPage);
	}
	
	public function goBackToMain1()
	{
		removeChild(wellDoneOneP);
		addChild(mainpage);
	}
	
	public function goToMainPage()
	{
		removeChild(instructionsPageShapes);
		addChild(shapesPage);
	}
	
	public function goToInstructionsPage()
	{
		instructionsPageShapes= new instructionsShapes(this);
		removeChild(shapesPage);
		addChild(instructionsPageShapes);
	}
	
	public function goToBoard3()
	{
		gameLetters= new lettersGame(this);
		removeChild(pageSelectBoard);
		addChild(gameLetters);
	}


	public function backBoard3()
	{
		removeChild(gameLetters);
		addChild(pageSelectBoard);
	}
	
	public function backToMainMenuPlayers()
	{
		removeChild(gameTwoPlayers);
		addChild(playersPage);
	}
	
	public function wellDTwo()
	{
		scoreIndex=gameTwoPlayers.indication;
		trace("Score index is: "+ scoreIndex);
		wellD= new wellDoneTwoPlayers(this);
		removeChild(gameTwoPlayers);
		addChild(wellD);
	}
	
	public function getScoreIndex()
	{
		trace("Score index in funtion get is: " + scoreIndex);
		return scoreIndex;
	}
	public function playAgain22()
	{
		removeChild(wellD);
		addChild(shapesPage);
	}
	
	public function goBackToMain22()
	{
		removeChild(wellD);
		addChild(mainpage);
	}
	
	
	//////////////////////////////////////////
	//Methods for returning both index:
	public function getIndexP1():int
	{
		trace("The index of player's 1 avatar");
		trace(indexP1);
		return indexP1;
	}
	
	public function getIndexP2():int
	{
		trace("The index of player's 2 avatar");
		trace(indexP2);
		return indexP2;
	}
	
	public function getTheText1():String
	{
		//trace("The text when called is");
		//trace(theText);
		return theText1;
	}
	public function getTheText2():String
	{
		//trace("The text when called is");
		//trace(theText);
		return theText2;
	}
	public function getMode():String
	{
		//trace("The text when called is");
		//trace(theText);
		return modeP;
	}
	
	////////////////////////////////////////////
	
}
}